package com.study.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.study.dao.PermissionDao;
import com.study.dao.RoleDao;
import com.study.dao.UserDao;
import com.study.pojo.Permission;
import com.study.pojo.Role;
import com.study.pojo.User;
import com.study.service.UserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Resource(name = "userDao")
    private UserDao userDao;
    @Resource(name = "roleDao")
    private RoleDao roleDao;
    @Resource(name = "permissionDao")
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }
        Set<Role> roleList = roleDao.findByUserId(user.getId());
        if(roleList != null && roleList.size() > 0){
            user.setRoles(roleList);
            for (Role role : roleList) {
                Set<Permission> permissionList = permissionDao.findByRoleId(role.getId());
               if(permissionList != null && permissionList.size() > 0){
                   role.setPermissions(permissionList);
               }
            }
        }
        return user;
    }
}
